﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RedSand.Site.Models;
using RedSand.Site.Controllers;
using RedSand.Web.Mvc;

namespace Site.Controllers
{
    public class CategoryController : BaseController
    {
        public ActionResult Manager()
        {
            return View(new ManagerModel { Title = "Category manager" });
        }

        [AjaxOrChildAction]
        public ActionResult List(int parent = 0)
        {
            ViewBag.ParentID = parent;
            return PartialView("List", RsDataManager.GetList<CategoryInfo>("CategoryGetList", new { ParentID = parent }));
        }
                                
        [AjaxOrChildAction, HttpGet]
        public ActionResult Create(int count, int parent = 0)
        {
            return PartialView("Create", new CategoryInfo { Order = count + 1, ParentID = parent });
        }
        [AjaxOrChildAction, HttpPost]
        public ActionResult Create(CategoryInfo info, int count)
        {
            if (ModelState.IsValid)
            {
                int result = RsDataManager.Save("CategorySave", info);
                if (result > 0)
                {
                    AddMessage(Message.CreateSuccess);
                    return RedirectToAction("List", new { parent = info.ParentID });
                }
                else
                {
                    return AjaxMessage(Message.CreateError);
                }
            }
            return AjaxModelError();
        }

        [AjaxOrChildAction, HttpGet]
        public ActionResult Edit(int id, int count)
        {
            ViewBag.Count = count;
            var info = RsDataManager.GetInfo<CategoryInfo>("CategoryGetInfo", "CategoryID", id);
            return PartialView("Edit", info);
        }
        [AjaxOrChildAction, HttpPost]
        public ActionResult Edit(CategoryInfo info, string NewImage)
        {
            if (ModelState.IsValid)
            {
                if (RsDataManager.Save("CategorySave", info) > 0)
                {
                    AddMessage(Message.UpdateSuccess);
                    return RedirectToAction("List", new { parent = info.ParentID });
                }
                else
                {
                    return AjaxMessage(Message.UpdateError);
                }
            }
            return AjaxModelError();
        }

        [AjaxOrChildAction]
        public ActionResult Delete(int id, int parent = 0)
        {
            var childs = RsDataManager.GetList<CategoryInfo>("CategoryGetList", new { ParentID = id });
            if (childs != null)
            {
                return AjaxMessage("Cannot delete item which has child item");
            }
            else
            {
                if (RsDataManager.Delete("CategoryDelete", "CategoryID", id))
                {
                    AddMessage(Message.DeleteSuccess);
                    return RedirectToAction("List", new { parent = parent });
                }
                return AjaxMessage(Message.DeleteError);
            }
        }

        [AjaxOrChildAction]
        public MvcHtmlString GetName(int id)
        {
            var info = RsDataManager.GetInfo<CategoryInfo>("CategoryGetInfo", "CategoryID", id);
            if (info == null) return MvcHtmlString.Empty;
            return MvcHtmlString.Create(info.Name);
        }
        
        
        [AjaxOrChildAction]
        public ActionResult feListCategory()
        {
            var LstCategory = RsDataManager.GetList<CategoryInfo>("CategoryGetList", new { ParentID = 0 });
            return PartialView(LstCategory);
        }
       

        [AjaxOrChildAction]
        public ActionResult feGetChildCategory(int ParentID)
        {
            ViewBag.AliasParentName = RsDataManager.GetInfo<CategoryInfo>("CategoryGetInfo", new { CategoryID = ParentID }).Alias;
            var LstCategory = RsDataManager.GetList<CategoryInfo>("CategoryGetList", new { ParentID = ParentID });
            return PartialView(LstCategory);
        }
        

        public MvcHtmlString feGenerateCategoryLink(int category)
        {
            var info = RedSand.Web.Mvc.RsDataManager.GetInfo<RedSand.Site.Models.CategoryInfo>("CategoryGetInfo", new { CategoryID = category });
            var link1 = string.Format("<a href='/{0}'>{1}</a>", info.Alias.ToLower(), info.Name);
            var link2 = "";
            if (info.ParentID.HasValue && info.ParentID > 0)
            {
                var parent = RedSand.Web.Mvc.RsDataManager.GetInfo<RedSand.Site.Models.CategoryInfo>("CategoryGetInfo", new { CategoryID = info.ParentID });
                link1 = string.Format("<a href='/{0}'>{1}</a>", parent.Alias.ToLower(), parent.Name);
                link2 = "/" + parent.Alias.ToLower() + "/" + info.Alias.ToLower();
                link2 = string.Format("  » <a href='{0}'>{1}</a>", link2, info.Name);
            }

            return MvcHtmlString.Create(link1 + link2);
        }
    }
}
